package cn.qidd.keep.manage.controller.system;

import cn.qidd.keep.domain.core.Result;
import cn.qidd.keep.domain.web.BaseController;
import cn.qidd.keep.manage.model.system.JobModel;
import cn.qidd.keep.manage.model.system.JobParams;
import cn.qidd.keep.manage.model.system.TriggerModel;
import cn.qidd.keep.manage.model.system.TriggerParams;
import cn.qidd.keep.manage.service.system.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by sunyanping on 2018/10/31
 */
@RestController
@RequestMapping("/api/task")
@Api(value = "TaskController", description = "定时任务")
public class TaskController extends BaseController {

    @Autowired
    private TaskService taskService;

    @GetMapping("/findTaskByPage")
    @ApiOperation(value = "分页查询定时任务", notes = "分页查询定时任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", defaultValue = "1", dataType = "integer", paramType = "query",
                    value = "页数 (1..N)"),
            @ApiImplicitParam(name = "size", defaultValue = "10", dataType = "integer", paramType = "query",
                    value = "每页大小"),
            @ApiImplicitParam(name = "sort", dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名1 asc,属性名2 desc. ")
    })
    public Result<Page<JobModel>> findTaskByPage(@ApiIgnore Pageable pageable) throws SchedulerException {
        List<JobModel> modelList = taskService.findAllJob();
        List<JobModel> collect = modelList.stream().skip(pageable.getPageNumber() * pageable.getPageSize())
                .limit(pageable.getPageSize()).collect(Collectors.toList());
        return new Result<>(Result.Status.SUCCESS, new PageImpl<>(collect));
    }

    @PostMapping("/updateJob")
    @ApiOperation(value = "修改任务信息", notes = "修改任务信息")
    public Result updateJob(@Validated @RequestBody JobParams params) throws SchedulerException {
        taskService.updateJob(params);
        return new Result(Result.Status.SUCCESS, "操作成功");
    }

    @PostMapping("/pauseJob")
    @ApiOperation(value = "暂停任务", notes = "暂停任务")
    public Result pauseJob(@Validated @RequestBody JobParams params) throws SchedulerException {
        taskService.pauseJob(params);
        return new Result(Result.Status.SUCCESS, "操作成功");
    }

    @PostMapping("/resumeJob")
    @ApiOperation(value = "恢复任务", notes = "恢复任务")
    public Result resumeJob(@Validated @RequestBody JobParams params) throws SchedulerException {
        taskService.resumeJob(params);
        return new Result(Result.Status.SUCCESS, "操作成功");
    }

    @PostMapping("/deleteJob")
    @ApiOperation(value = "删除任务", notes = "删除任务")
    public Result deleteJob(@Validated @RequestBody JobParams params) throws SchedulerException {
        taskService.deleteJob(params);
        return new Result(Result.Status.SUCCESS, "操作成功");
    }

    @PostMapping("/runJob")
    @ApiOperation(value = "立即执行", notes = "立即执行")
    public Result runJob(@Validated @RequestBody JobParams params) throws SchedulerException {
        taskService.runJob(params);
        return new Result(Result.Status.SUCCESS, "操作成功");
    }

    @GetMapping("/getAllTriggerOfJob")
    @ApiOperation(value = "获取任务触发器列表", notes = "获取任务触发器列表")
    public Result<List<TriggerModel>> getAllTriggerOfJob(@RequestParam String jobName, @RequestParam String jobGroup) throws SchedulerException {
        List<TriggerModel> allTriggerOfJob = taskService.getAllTriggerOfJob(jobName, jobGroup);
        return new Result<>(Result.Status.SUCCESS, allTriggerOfJob);
    }

    @PostMapping("/addTrigger")
    @ApiOperation(value = "添加触发器", notes = "添加触发器")
    public Result addTrigger(@Validated @RequestBody TriggerParams triggerParams) throws SchedulerException {
        taskService.addTrigger(triggerParams);
        return new Result(Result.Status.SUCCESS, "操作成功");
    }

    @PostMapping("/deleteTrigger")
    @ApiOperation(value = "移除触发器", notes = "移除触发器")
    public Result updateTrigger(@Validated @RequestBody TriggerParams triggerParams) throws SchedulerException {
        taskService.deleteTrigger(triggerParams);
        return new Result(Result.Status.SUCCESS, "操作成功");
    }

    @PostMapping("/pauseTrigger")
    @ApiOperation(value = "暂停触发器", notes = "暂停触发器")
    public Result pauseTrigger(@Validated @RequestBody TriggerParams triggerParams) throws SchedulerException {
        taskService.pauseTrigger(triggerParams);
        return new Result(Result.Status.SUCCESS, "操作成功");
    }

    @PostMapping("/resumeTrigger")
    @ApiOperation(value = "恢复触发器", notes = "恢复触发器")
    public Result resumeTrigger(@Validated @RequestBody TriggerParams triggerParams) throws SchedulerException {
        taskService.resumeTrigger(triggerParams);
        return new Result(Result.Status.SUCCESS, "操作成功");
    }
}
